


%GENERATE TABLE 4;
clear all; addpath('auxiliar'); addpath('Simulated_Datasets');
%Model With exogenous constraints
load DE_exo
momsDE_exo=moms;
mbindDE_exo=mean(bindSIM)*100;
mSSDE_exo=mean(SS)*100;
mDefDE_exo= mean(DefSIM)*100;

clearvars -except momsDE_exo mbindDE_exo mSSDE_exo mDefDE_exo

load SP_15k_exo.mat
 VSP=V;
 bgrid_SP=bgrid_large;
QSP=eQ_temp_large;

bpSP=bp;
moments_inorder=[mean(NFA),avPvD,avPub,avSpr,std(NFA),a_Std_PvDebt,a_Std_PubDebt,a_Std_Spr]
moms=[avSpr,a_Std_Spr,avPvD,avPub, a_Std_PvDebt, a_Std_PubDebt,mean(NFA),std(NFA)];
 clear bpSIM VSIM bSIM V BGpSIM BGSIM S_index BGiSIM bgrid Q bp
 
 load simDE_EXO.mat

 
[T,~]=size(VSIM);
VSPSIM=zeros(T,1);
N=1;


parfor i=1:T
    for j=1:N
    VSPSIM(i,j)=interp1(squeeze(bgrid(1,:)),squeeze(VSP(BGiSIM(i,j),:,S_index(i))),bSIM(i,j),'linear','extrap');
    end
end

Wf_Gains=((VSPSIM*(1-sigma)*(1-beta)+ones(T,1))./(VSIM*(1-sigma)*(1-beta)+ones(T,1))).^(1/(1-sigma))-ones(T,1);

momsSP_exo=moms;
mbindSP_exo=mean(bindSIM)*100;
mSSSP_exo=mean(SS)*100;
mDefSP_exo= mean(DefSIM)*100;
welfare_exo=mean(Wf_Gains)*100;

clearvars -except momsDE_exo mbindDE_exo mSSDE_exo mDefDE_exo momsSP_exo mbindSP_exo mSSSP_exo mDefSP_exo welfare_exo

%MODEL WITH NOT DEFAULT COST
load DE_nodefaultcost
momsDE_ncost=moms;
mbindDE_ncost=mean(bindSIM)*100;
mSSDE_ncost=mean(SS)*100;
mDefDE_ncost= mean(DefSIM)*100;

clearvars -except momsDE_exo mbindDE_exo mSSDE_exo mDefDE_exo momsSP_exo mbindSP_exo mSSSP_exo mDefSP_exo welfare_exo momsDE_ncost mbindDE_ncost mSSDE_ncost mDefDE_ncost

load SP_SIMULATION_NoDCost.mat
 VSP=V;
 bgrid_SP=bgrid_large;
QSP=eQ_temp_large;

bpSP=bp;
moments_inorder=[mean(NFA),avPvD,avPub,avSpr,std(NFA),a_Std_PvDebt,a_Std_PubDebt,a_Std_Spr]
moms=[avSpr,a_Std_Spr,avPvD,avPub, a_Std_PvDebt, a_Std_PubDebt,mean(NFA),std(NFA)];
 clear bpSIM VSIM bSIM V BGpSIM BGSIM S_index BGiSIM bgrid Q bp
 
 load simDE_nocost.mat

 
[T,~]=size(VSIM);
VSPSIM=zeros(T,1);
N=1;


parfor i=1:T
    for j=1:N
    VSPSIM(i,j)=interp1(squeeze(bgrid(1,:)),squeeze(VSP(BGiSIM(i,j),:,S_index(i))),bSIM(i,j),'linear','extrap');
    end
end

Wf_Gains=((VSPSIM*(1-sigma)*(1-beta)+ones(T,1))./(VSIM*(1-sigma)*(1-beta)+ones(T,1))).^(1/(1-sigma))-ones(T,1);

momsSP_ncost=moms;
mbindSP_ncost=mean(bindSIM)*100;
mSSSP_ncost=mean(SS)*100;
mDefSP_ncost= mean(DefSIM)*100;
welfare_ncost=mean(Wf_Gains)*100;

clearvars -except momsDE_exo mbindDE_exo mSSDE_exo mDefDE_exo momsSP_exo mbindSP_exo mSSSP_exo mDefSP_exo welfare_exo  momsDE_ncost mbindDE_ncost mSSDE_ncost mDefDE_ncost momsSP_ncost mbindSP_ncost mSSSP_ncost mDefSP_ncost welfare_ncost
                


%% BASELINE
 load SIMULATION_ERGODIC_DE.mat;
momsDE=moms;
mbindDE=mean(bindSIM)*100;
mSSDE=mean(SS)*100;
mDefDE= mean(DefSIM)*100;
load SP_SIMULATION_20k


VSP=V;
bgrid_SP=bgrid_large;
QSP=eQ_temp_large;

bpSP=bp;
moments_inorder=[mean(NFA),avPvD,avPub,avSpr,std(NFA),a_Std_PvDebt,a_Std_PubDebt,a_Std_Spr];
moms=[avSpr,a_Std_Spr,avPvD,avPub, a_Std_PvDebt, a_Std_PubDebt,mean(NFA),std(NFA)];
 clear bpSIM VSIM bSIM V BGpSIM BGSIM S_index BGiSIM bgrid Q bp
 
 load simDE.mat

[T,~]=size(VSIM);
VSPSIM=zeros(T,1);
N=1;


parfor i=1:T
    for j=1:N
    VSPSIM(i,j)=interp1(squeeze(bgrid(1,:)),squeeze(VSP(BGiSIM(i,j),:,S_index(i))),bSIM(i,j),'linear','extrap');
    end
end

Wf_Gains=((VSPSIM*(1-sigma)*(1-beta)+ones(T,1))./(VSIM*(1-sigma)*(1-beta)+ones(T,1))).^(1/(1-sigma))-ones(T,1);

mean(Wf_Gains)*100;


 [num,tex] = xlsread('targets.xls','Sheet1');
targets=[mean(num(:,5)), std(num(:,9)), mean(num(:,2)), mean(num(:,3)),std(num(:,6)), std(num(:,7)),mean(num(:,4)),std(num(:,8))];

%%
 matrix={'Private debt', momsDE(3),moms(3), momsDE_exo(3), momsSP_exo(3), momsDE_ncost(3), momsSP_ncost(3)  ;
         'Public debt', momsDE(4),moms(4), momsDE_exo(4), momsSP_exo(4), momsDE_ncost(4), momsSP_ncost(4)  ;
         'Mean spread',momsDE(1), moms(1), momsDE_exo(1), momsSP_exo(1), momsDE_ncost(1), momsSP_ncost(1)  ;
 'Probability of a binding constraint', mbindDE, mean(bindSIM)*100, mbindDE_exo, mbindSP_exo, mbindDE_ncost, mbindSP_ncost  ;
 'Probability of a financial crisis', mSSDE, mean(SS)*100, mSSDE_exo, mSSSP_exo, mSSDE_ncost, mSSSP_ncost  ;
 'Probability of default',mDefDE, mean(DefSIM)*100, mDefDE_exo, mDefSP_exo, mDefDE_ncost, mDefSP_ncost  ;
 'Welfare gains','', mean(Wf_Gains)*100, '' , welfare_exo,'',welfare_ncost};
  columnLabels = {'Moments','Baseline','Planner','Baseline','Planner','Baseline','Planner'};

  matrix2latex(matrix, 'Tables\Table_4.tex', 'columnLabels', columnLabels, 'alignment', 'c','format', '%-6.2f');